﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Search;
using System.Net.Mail;
using System.Net.Mime;
using System.Collections;
using DotNetNuke.Entities.Users;
using System.Globalization;
namespace Triax.Modules.TriaxStoreView
{
    /// -----------------------------------------------------------------------------
    ///<summary>
    /// The Controller class for the TriaxStoreViewController
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public class TriaxStoreViewController 
    {
         public  PetaPoco.Database db;// = new PetaPoco.Database(ConfigurationManager.AppSettings["SiteSqlServer"], "System.Data.SqlClient");
         private static TriaxStoreViewController _Instance;
         public static TriaxStoreViewController Instance
         {
             get
             {
                 if (_Instance == null)
                 {
                     _Instance = new TriaxStoreViewController();
                     _Instance.db = new PetaPoco.Database(ConfigurationManager.AppSettings["SiteSqlServer"], "System.Data.SqlClient");
                 }

                 return _Instance;
             }
         }
        public static  DateTime convertNgay(string ngaybatdau)
         {
             try
             {
                 string[] dtemp = ngaybatdau.Split('/');
                 if (dtemp.Length > 2)
                 {
                     return DateTime.ParseExact(ngaybatdau, "d/M/yyyy", CultureInfo.InvariantCulture);
                 }
             }
             catch
             {
                 return DateTime.Parse("01/01/1900");
             }
             return DateTime.Parse("01/01/1900");
         }
        public static int convertInt(string text)
        {
            int res = 0;
            try
            {
                int.TryParse(text, out res);
            }
            catch
            {
                res = 0;
            }
            return res;
        }

        public static float convertFloat(string text)
        {
            float res = 0;
            try
            {
                float.TryParse(text, out res);
            }
            catch
            {
                res = 0;
            }
            return res;
        }
    #region Constructors

        public TriaxStoreViewController()
        {
        }

    #endregion
       
    #region Public Methods


       
     
        public string getNguoitao(string id)
        {
            string res = "";
            // Response.Write("<br/>" + id);
            DotNetNuke.Entities.Users.UserInfo user = DotNetNuke.Entities.Users.UserController.GetUserById(0, int.Parse(id));
            if (user != null)
                return user.DisplayName;
            return res;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// adds an object to the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objTriaxStoreViewController">The TriaxStoreViewController object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
     
        // End kết hợp xử lý
    #endregion

   
        public bool checkRole(string id, string rolelist)
        {
            bool res = false;
            string[] list = rolelist.Split(';');
            DotNetNuke.Entities.Users.UserInfo USERINFO = DotNetNuke.Entities.Users.UserController.GetUserById(0, int.Parse(id));
            for (int i = 0; i < list.Length; i++ )
                if (USERINFO.IsInRole(list[i]))
                {
                    return true;
                }
            return res;
        }
        public int getType(string id)
        {
            int res = 0;
            string rolelist = "bangiamdoc;phongbientap;phongcongnghe;phonghanhchinh;phongkehoach;phongketoan;phongkinhdoanh;phongphathanh;vanphong;truongphongbt";
            string[] list = rolelist.Split(';');
            DotNetNuke.Entities.Users.UserInfo USERINFO = DotNetNuke.Entities.Users.UserController.GetUserById(0, int.Parse(id));
            for (int i = 0; i < list.Length; i++)
                if (USERINFO.IsInRole(list[i]))
                {
                    return (i+1);
                }
            return res;
        }

        public bool sendMai(string email, string title, string noidung)
        {
            // <add key="EmailName" value="sell.huyhoang@gmail.com" />
            //<add key="EmailPassword" value="huyhoang111111" />
            //<add key="EmailHost" value="smtp.gmail.com" />
            //  <add key="EmailPort" value="587" />
            bool check = true;
            string[] mails = email.Split(';');
            string EmailName = "senwebthongbao@gmail.com";//ConfigurationSettings.AppSettings["EmailName"];
            string EmailPassword = "senwebthongbao123456";// ConfigurationSettings.AppSettings["EmailPassword"];
            string EmailHost = "smtp.gmail.com";// ConfigurationSettings.AppSettings["EmailHost"];
            string EmailPort = "587";// ConfigurationSettings.AppSettings["EmailPort"];
            string res = "";
            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential(EmailName, EmailPassword);
            SmtpServer.Port = int.Parse(EmailPort);
            SmtpServer.Host = EmailHost;
            SmtpServer.EnableSsl = false;
            MailMessage mail = new MailMessage();
            //mail.BodyEncoding
            // string image = ImageFile.ImageUrl;
            // string path = "http://huyhoangbook.vn/" + image;
            try
            {
                mail.From = new MailAddress(EmailName, "TTNB Thông báo", System.Text.Encoding.UTF8);
                Byte i;

                mail.To.Add("nammct@gmail.com");
                for (int ii = 0; ii < mails.Length; ii++)
                {
                    mail.Bcc.Add(mails[ii]);
                }
                mail.Subject = title;
                //LinkedResource logo = new LinkedResource(path);
                //logo.ContentId = "companylogo";
                // AlternateView alternateView1 = AlternateView.CreateAlternateViewFromString(noidung, null, MediaTypeNames.Text.Html);
                //   alternateView1.LinkedResources.Add(logo);
                //mail.AlternateViews.Add(alternateView1);

                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress(EmailName);
                //   mail.Bcc.Add(new MailAddress(EmailName));
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                return false;
                //     MessageBox.Show(ex.ToString());
                 //   Response.Write("Khong  thể gửi thư " + ex.ToString());
            }
            return check;
        }


    }
}

